import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import UserRegisterLoginView from '@/views/UserRegisterLoginView.vue'
import UserRegisterForm from '@/components/User/Form/UserRegisterForm.vue'
import UserLoginForm from '@/components/User/Form/UserLoginForm.vue'
import UserApplyshopForm from '@/components/User/Form/UserApplyshopForm.vue'
import UserRALHeader from '@/components/User/UserRALHeader.vue'
import UserApplyshopHeader from '@/components/User/UserApplyshopHeader.vue'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      beforeEnter:(to,from,next)=>{
        next();
      }
    },
    {
      path: '/userinfoget',
      name: 'userinfoget',
      component: UserRegisterLoginView,
      children: [
        {
          path: 'register',
          name: 'register',
          components: {
            header:UserRALHeader,
            form:UserRegisterForm
          }
        },
        {
          path: 'login',
          name:'login',
          components:{
            header:UserRALHeader,
            form:UserLoginForm
          }
        },
        {
          path: 'applyshop',
          name:'applyshop',
          components:{
            header:UserApplyshopHeader,
            form:UserApplyshopForm
          },
          beforeEnter:(to,form,next)=>{
            const user_info = JSON.parse((localStorage.getItem('user_info')||"{}"))
            if(user_info.token){
              next()
            }
            else{
              alert('请先登录')
              router.push('/userinfoget/login')
              next(false)
            }
          }
          
        },
        {  
          path: '',  
          redirect: 'login'
        }  
      ]
    },
  ]
})

export default router
